<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/common.css"/>
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/jquery-ui-timepicker-addon.css"/>
    <link rel="stylesheet" href="../css/jquery-ui.css"/>
    <link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="../css/city-picker.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="../css/jquery-impromptu.min.css" type="text/css"/>
    <title></title>
</head>
<body>
<div class="container">
    <div class="box">
        <div class="box_border" style="height: 100%;">
            <div class="box_top"><b class="pl15">数据筛选</b></div>
            <div class="box_center pt10">
                <table id="approval_info1" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.2rem">
                    <tr>
                        <th class="td_center" style="width: 5%;" >序号</th>
                        <th class="td_center" >部门</th>
                        <th class="td_center" >权限等级</th>
                        <th class="td_center" >财务权限</th>
                        <th class="td_center" >业务权限</th>
                        <th class="td_center" >操作</th>
                    </tr>
                    <tbody id="appendInfo">

                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="6"> <div class="fr"><a class="ext_btn"  onclick="addLeaderSection()"  id="add"><span class="add"></span>添加</a></div></td>
                    </tr>
                    </tfoot>
                </table>

                </div>
        </div>
    </div>
</div>
<div id="edit" style="display: none;">
    <form name="from1" >
        <div class="pb10 mb15 " > <div style="display:inline-block;width:60px;text-align: left;line-height: 200%;" >权限等级:</div>
            <select name="power_level" class="select"  style="width: 150px;">
                <option value="1">部门审批</option>
                <option value="2">分管签批</option>
                <option value="3">领导签批</option>
            </select>
        </div>
        <div class="pb10" > <div style="display: inline-block;width:60px;text-align: right;" >拥有权限:</div>
            <input name="is_approve" type="checkbox"  style="vertical-align: middle;margin-top: -4px">&nbsp;财务权限
                <input name="is_fee" type="checkbox" style="vertical-align: middle;margin-top: -4px">&nbsp;业务权限</div>
    </form>
</div>
<div id="addSection" style="display: none;">
    <form name="from2" >
        <div class="pb10 mb15 " > <div style="display:inline-block;width:60px;text-align: left;line-height: 200%;" >权限等级:</div>
            <select id="addSectionSelect" name="section_id1" class="select"  style="width: 150px;">
            </select>
        </div>
        <div class="pb10 mb15 " > <div style="display:inline-block;width:60px;text-align: left;line-height: 200%;" >权限等级:</div>
            <select name="power_level1" class="select"  style="width: 150px;">
                <option value="1">部门审批</option>
                <option value="2">分管签批</option>
                <option value="3">领导签批</option>
            </select>
        </div>
        <div class="pb10" > <div style="display: inline-block;width:60px;text-align: right;" >拥有权限:</div>
            <input name="is_approve1" type="checkbox"  style="vertical-align: middle;margin-top: -4px">&nbsp;财务权限
            <input name="is_fee1" type="checkbox" style="vertical-align: middle;margin-top: -4px">&nbsp;业务权限</div>
    </form>
</div>
<div id="info" style="text-align: center;display: none;"></div>
</body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/config.js"></script>
<script>
    //获取所有部门部门基本信息
    function getAllSectionData(){
        var phpUrl=url+'Getsectionmanager.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            async:false,
            data:{model:'getAllSection'},
            success: function (data) {
                data=JSON.parse(data);
                if(data['state']==1){
                    section=data['data'];
                }
            }
        });
    }
    //加载表格
    function createTable(){
        var phpUrl=url+'Getleadermanager.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'getLeaderAllData',user_id:leaderUserId},
            success: function (allLeaderUserData) {
//                alert(allLeaderUserData);
                allLeaderUserData=JSON.parse(allLeaderUserData);
                var tableData=allLeaderUserData['data'];
                var tableContent=null;
                $("#appendInfo").empty();
                for(var i=0;i<tableData.length;i++) {
                    if(i%2==0)
                    tableContent= '<tr><td class="td_center  " >' + (i + 1) + '</td>';
                    else
                        tableContent= '<tr style="background-color: #eefefe;"><td class="td_center  " >' + (i + 1) + '</td>';
                    for(var j=0;j<section.length;j++){
                        if(section[j]['section_ID']==tableData[i]['section_id']) {
                            tableContent+='<td class="td_center  " >' + section[j]['section_name']+ '</td>';
                            break;
                        }
                    }
                    switch(tableData[i]['power_level']){
                        case '1': tableContent+= '<td class="td_center" >部门审批</td>';break;
                        case '2': tableContent+= '<td class="td_center" >分管签批</td>';break;
                        case '3': tableContent+= '<td class="td_center" >领导签批</td>';break;
                    }

                    if(tableData[i]['is_approve']==1)
                        tableContent+= '<td class="td_center" >有</td>';
                    else
                        tableContent+= '<td class="td_center" >无</td>';

                    if(tableData[i]['is_fee']==1)
                        tableContent+= '<td class="td_center" >有</td>';
                    else
                        tableContent+= '<td class="td_center" >无</td>';
                    tableContent+='<td class="td_center"><a class="ext_btn modify" onclick="editLeaderSectionData(' + tableData[i]['section_id'] + ')"><span class="modify" ></span>编辑</a>' +
                            '<a class="ext_btn del" onclick="deleteleaderSection('+tableData[i]['section_id']+')"><span class="del" ></span>删除</a></td></tr>';
                    $("#appendInfo").append(tableContent);
                }
            }
        });
    }
    //编辑信息
    function editLeaderSectionData(e){
        var phpUrl=url+'Getleadermanager.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:"getLeaderDataByUserAndSection",user_id:leaderUserId,section_id:e},
            success: function (data) {
//                alert(data);
                data=JSON.parse(data);
                var objForm1 = document.from1;
                if(data['state']==1) {
//                    alert(data['data'][0]['power_level']);
                    objForm1.power_level[data['data'][0]['power_level']-1].selected = true;
                    if(data['data'][0]['is_approve']==1) objForm1.is_approve.checked=true; else objForm1.is_approve.checked=false;
                    if(data['data'][0]['is_fee']==1) objForm1.is_fee.checked=true; else  objForm1.is_fee.checked=false;
                    $("#edit").dialog({
                        resizable:false,
                        title:data['data'][0]['section_name']+'-权限设置',
                        width:250,
                        modal: true,
                        buttons:{
                            '取消': function () {
                                $("#edit").dialog('close');
                            },
                            '确定':function(){
                                $("#edit").dialog('close');
                                var is_approve=0;
                                var is_fee=0;
                                if(objForm1.is_approve.checked==true) is_approve=1;
                                if(objForm1.is_fee.checked==true) is_fee=1;
                                $.ajax({
                                    url:phpUrl,
                                    type:"POST",
                                    data:{model:"updateLeaderDataByUserAndSection",user_id:leaderUserId,section_id:e,power_level:objForm1.power_level.value,is_approve:is_approve,is_fee:is_fee},
                                    success: function (state) {
//                                        alert(state);
                                        state=JSON.parse(state);
                                        if(state['state']==1)
                                            document.getElementById('info').innerHTML='保存成功!';
                                        else
                                            document.getElementById('info').innerHTML='保存失败!';
                                        $("#info").dialog({
                                            resizable: false,
                                            width: 250,
                                            title: "信息",
                                            modal: true,
                                            buttons: {
                                                '确定': function () {
                                                    $("#info").dialog('close');
                                                    createTable();
                                                }
                                            }
                                        });
                                    }
                                });
//                $("#edit").dialog('close');
                            }
                        }
                    });
                }
            }
        });
    }
    //删除部门
    function deleteleaderSection(e){
        var phpUrl=url+'Getleadermanager.php';
        document.getElementById('info').innerHTML="你确定要删除吗？";
        $("#info").dialog({
            resizable: false,
            width: 250,
            title: "信息",
            modal: true,
            buttons: {
                '取消': function () {
                    $("#info").dialog('close');
                },
                '确定': function () {
                    $.ajax({
                        url:phpUrl,
                        type:"POST",
                        data:{model:'deleteLeaderSectionByUserAndSection',user_id:leaderUserId,section_id:e},
                        success: function (state) {
//                            alert(state);
                            state=JSON.parse(state);
                            if(state['state']==1)
                                document.getElementById('info').innerHTML='删除成功!';
                            else
                                document.getElementById('info').innerHTML='删除失败!';
                            $("#info").dialog({
                                resizable: false,
                                width: 250,
                                title: "信息",
                                modal: true,
                                buttons: {
                                    '确定': function () {
                                        $("#info").dialog('close');
                                        createTable();
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
    }
    //添加部门
   function addLeaderSection(){
       var phpUrl=url+'Getleadermanager.php';
       $.ajax({
           url:phpUrl,
           type:"POST",
           async:false,
           data:{model:'getLeaderAllSectionId',user_id:leaderUserId},
           success: function (data) {
//               alert(data);
               data=JSON.parse(data);
//               alert(data['state']);
               if(data['state']==1){
                   if(data['data']){
                   var sectionData=data['data'];
//                   alert(data['data'].length);
                   document.getElementById("addSectionSelect").options.length = 0;
                   for(var i=0;i<data['data'].length;i++){
//                       alert('<option value="'+sectionData[i]['section_id']+'">'+sectionData[i]['section_name']+'</option>');
                   $("#addSectionSelect").append('<option value="'+sectionData[i]['section_id']+'">'+sectionData[i]['section_name']+'</option>');
                   }
                   $("#addSection").dialog({
                       resizable: false,
                       width: 250,
                       title: "添加部门权限",
                       modal: true,
                       buttons: {
                           '取消': function () {
                               $("#addSection").dialog('close');
                           },
                           '确定': function () {
                               $("#addSection").dialog('close');
//                               alert(1);
                               var objForm2 = document.from2;
                               var section_id=objForm2.section_id1.value;
                               var power_level=objForm2.power_level1.value;
                               var is_approve=0;
                               var is_fee=0;
                               if(objForm2.is_approve1.checked==true) is_approve=1;
                               if(objForm2.is_fee1.checked==true) is_fee=1;
                               $.ajax({
                                   url:phpUrl,
                                   type:"POST",
                                   data:{model:'insertLeaderSection',user_id:leaderUserId,section_id:section_id,power_level:power_level,is_approve:is_approve,is_fee:is_fee},
                                   success: function (state) {
                                       state=JSON.parse(state);
                                       if(state['state']==1)
                                           document.getElementById('info').innerHTML='添加成功!';
                                       else
                                           document.getElementById('info').innerHTML='添加失败!';
                                       $("#info").dialog({
                                           resizable: false,
                                           width: 250,
                                           title: "信息",
                                           modal: true,
                                           buttons: {
                                               '确定': function () {
                                                   $("#info").dialog('close');
                                                   createTable();
                                               }
                                           }
                                       });
                                   }
                               });
//                               alert(section_id);
//                               $("#addSction").dialog('close');
//                               createTable();
                           }
                       }
                   });
               }
           }
       }
       });
    }

    var section=null;
    var leaderUserId=null;
    $(function () {
        leaderUserId=window.localStorage.getItem('leaderUserId');
//        alert(leaderUserId);
        getAllSectionData();
        createTable();
    })
</script>
</html>